<?php


namespace ba;

use app\exceptions\ApiException;

class ScenicApi
{

    const LIST_URL = 'https://route.showapi.com/1681-1';

    const DETAILS_URL = 'https://route.showapi.com/1681-2';


    public function searchScenic($scenic_name,$page = 1,$pageSize=10){
        $config = config('setting.wwxt');
        $data = [
            'appKey' => $config['appKey'],
            'pageSize' => $pageSize,
            'key' => urlencode($scenic_name),
            'page' => $page
        ];

        $response = sendPostRequest(self::LIST_URL, $data);
        $result = json_decode($response,true);

        if($result['showapi_res_code'] == 0 && $result['showapi_res_body']['ret_code'] == 0){
            return $result['showapi_res_body'];
        }
        throw new ApiException($result['showapi_res_body']['msg']);
    }



    public function getDetail($scenicId){
        $config = config('setting.wwxt');
        $data = [
            'appKey' => $config['appKey'],
            'scenicId' => $scenicId
        ];
        $response = sendPostRequest(self::DETAILS_URL, $data);
        $result = json_decode($response,true);
        if($result['showapi_res_code'] == 0 && $result['showapi_res_body']['ret_code'] == 0){
            return $result['showapi_res_body'];
        }
        return false;
    }

}
